/** 
 * <p>Original Author:  jessefreeman</p>
 * <p>Class File: AbstractCamoBitmap.as</p>
 * 
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 * 
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 * 
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 * 
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 * 
 * <p>Revisions<br/> 
 * 	2.0  Initial version Jan 7, 2009</p>
 *
 */
 
package camo.core.decal {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.errors.IllegalOperationError;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;	

	public class AbstractCamoBitmap extends Bitmap {

		/**
		 * <p>The base extension of the Bitmap class that allows the support of
		 * BitmapData sampling.</p>
		 * 
		 * @param self
		 * @param bmpData
		 * @param pixelSnapping
		 * @param smoothing
		 * 
		 */		
		public function AbstractCamoBitmap(self:AbstractCamoBitmap, bmpData : BitmapData = null, pixelSnapping : String = "auto", smoothing : Boolean = false) 
		{
			// super constructor
			if(self is AbstractCamoBitmap)
            {
            	super(bmpData, pixelSnapping, smoothing);	
            }
            else
            {
                //only a subclass can pass a valid reference to self
                throw new IllegalOperationError("AbstractDecal cannot be instantiated directly.");
            }
		}
		
		/**
		 * <p>This allows you to "sample" an area of the Decal based on the
		 * Rectangle you pass in. Use this to cut out a specific section of
		 * Bitmapdata.</p>
		 * 
		 * @param rect
		 * @return 
		 * 
		 */		 
		public function sample(rect:Rectangle):BitmapData
		{
			var tx:Number = rect.x;
			var ty:Number = rect.y;
			var w:Number = rect.width;
			var h:Number = rect.height;
			// Applies the correct offset when sampeling the data
			var m:Matrix = new Matrix();
				m.translate(-tx,-ty);
			
			//TODO this should use copyBitmapData
			// Creates new BitmapData				
			var bmd:BitmapData = new BitmapData(w,h, true, 0x000000);
				bmd.draw(bitmapData, m);
			return bmd;
		}
		
	}
}